/**
 * 
 */
package net.toocruel.iqismart.service.impl;


import net.toocruel.iqismart.dao.ResourceRepository;
import net.toocruel.iqismart.dao.RoleRepository;
import net.toocruel.iqismart.dao.spec.RoleSpec;
import net.toocruel.iqismart.entity.rbac.Resource;
import net.toocruel.iqismart.entity.rbac.Role;
import net.toocruel.iqismart.dao.condition.RoleCondition;
import net.toocruel.iqismart.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author toocruel
 *
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleRepository roleRepository;
	
	@Autowired
	private ResourceRepository resourceRepository;
	
	/* (non-Javadoc)
	 * @see net.toocruel.iqismart.security.rbac.service.RoleService#create(net.toocruel.iqismart.security.rbac.dto.RoleInfo)
	 */
	@Override
	public Role create(Role role) {
		role.setId(roleRepository.save(role).getId());
		return role;
	}

	/* (non-Javadoc)
	 * @see net.toocruel.iqismart.security.rbac.service.RoleService#update(net.toocruel.iqismart.security.rbac.dto.RoleInfo)
	 */
	@Override
	public Role update(Role role) {
//		Role role = roleRepository.findOne(info.getId());
//		BeanUtils.copyProperties(info, role,new String[]{"createdAt"});
		roleRepository.save(role);
		return role;
	}

	@Override
	public void delete(Long id) {
		Role role = roleRepository.findOne(id);
//		if(CollectionUtils.isNotEmpty(role.getUsers())){
//			throw new RuntimeException("不能删除有下挂用户的角色");
//		}
		roleRepository.delete(id);
	}
//
//	@Override
//	public String[] getRoleMenus(Long id) {
//		return StringUtils.split(roleRepository.findOne(id).getMenus(), ",");
//	}
//
//	/**
//	 * (non-Javadoc)
//	 * @see com.idea.ams.service.RoleService#setRoleMenu(java.lang.Long, java.lang.String)
//	 */
//	@Override
//	public void setRoleMenu(Long roleId, String menuIds) {
//		Role role = roleRepository.findOne(roleId);
//		role.setMenus(menuIds);
//	}


	@Override
	public Role getInfo(Long id) {
		return roleRepository.findOne(id);
	}

	/* (non-Javadoc)
	 * @see net.toocruel.iqismart.security.rbac.service.RoleService#findAll()
	 */
	@Override
	public List<Role> findAll() {
//		return QueryResultConverter.convert(roleRepository.findAll(), RoleInfo.class);
		return roleRepository.findAll();
	}



	@Override
	public Page<Role> query(RoleCondition condition, PageRequest pageable) {
		return  roleRepository.findAll(new RoleSpec(condition), pageable);
//		return QueryResultConverter.convert(roles, RoleInfo.class, pageable);
	}

	@Override
	public Role findById(Long id) {
		return roleRepository.findOne(id);
	}

	@Override
	public int deleteMany(Long[] id) {
		return roleRepository.deleteByIdIn(id);
	}

	@Override
	public void setResources(Role role) {
		Role _role = roleRepository.findOne(role.getId());

		Long[] resourceIds = role.getResourceIds();
		List<Resource> resources = resourceRepository.findByIdIn(resourceIds);
		_role.getResources().clear();
		_role.getResources().addAll(resources);

		roleRepository.save(_role);
	}
}
